package in.inishant.common.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/movie")
public class MovieController {

	@RequestMapping(value="/{name}", method = RequestMethod.GET)
	public String getMovie(@PathVariable String name, ModelMap model,HttpServletRequest req) {
		
		System.out.println("File entry");
		String test2 = req.getSession().getServletContext().getRealPath("/");
		File file= new File(getRelativePath(test2,2));
		System.out.println("test2: " + test2);
		if(file.exists()){
			try {
				System.out.println("File "+ file.getCanonicalPath());
				File[] a=file.listFiles();
				for(int i=0;i<a.length;i++)
					System.out.println(a[i].getCanonicalPath());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("File exit");
		model.addAttribute("movie", name);
		return "list";

	}
	
	private  String getRelativePath(String test2, int i) {
		String[] x=test2.split("[/\\\\]");
		String z="";
		for(int j=0;j<x.length-i;j++){
			System.out.println(x[j]);
			if("".equals(z)){
				z=x[j];
			}else{
				z=z+File.separator+x[j];
			}
		}
		return z;
	}
	
	/*public static void main (String[] args){
		System.out.println(getRelativePath("c:\\\\a\\c\\d",2));
	}*/
	
	
	
}